<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="用户名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" style="width: 100%">
      <el-table-column
        prop="friendId"
        header-align="center"
        align="center"
        label="好友id"
      >
      </el-table-column>
      <el-table-column
        prop="friendName"
        header-align="center"
        align="center"
        label="好友"
      >
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="sendMessage(scope.row.friendId)"
            >发消息</el-button
          >
          <el-button type="text" size="small" @click="uploadFile(scope.row.friendId)"
            >上传附件</el-button
          >
          <el-button type="text" size="small" @click="messageList(scope.row.friendId)"
            >消息列表</el-button
          >
          <el-button type="text" size="small" @click="deleteHandle(scope.row.friendId)"
            >删除好友</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <message v-if="messageLoading" ref="message"></message>
    <messagelist v-if="listLoading" ref="messageListRef"></messagelist>
  </div>
</template>

<script>
import message from "@/components/friend/message.vue";
import messagelist from "@/components/friend/messagelist.vue";
export default {
  data() {
    return {
      dataForm: {
        key: "",
      },
      friendList: [],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      messageLoading: false,
      listLoading: false,
      dataListLoading: false,
      dataListSelections: [],
    };
  },
  components: {
    message,
    messagelist,
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/generator/userfriend/friendList"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          username: this.dataForm.key,
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 删除
    deleteHandle(id) {
      this.$confirm(`确定删除好友吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl(`/generator/userfriend/deleteFirend/${id}`),
          method: "get",
          data: this.$http.adornParams(),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
    uploadFile(id) {
      this.$message({
              message: "功能尚未开发,敬请期待",
              type: "success",
              duration: 1500,
            });
    },
    sendMessage(id) {
      this.messageLoading = true;
      this.$nextTick(() => {
        this.$refs.message.init(id);
      });
    },
    messageList(id) {
      this.listLoading = true;
      this.$nextTick(() => {
        this.$refs.messageListRef.init(id);
      });
    },
  },
};
</script>
